# Key Information --------------------------------------------------------------
# Title: Replication File
# Project: Marketing Taxation? Experimental Evidence on Enforcement and 
#          Bargaining in Malawian Markets.
# Purpose: This script reproduces all analysis for the project shown in the paper
#          and the supplementary appendix.
#
# To replicate the project from scratch (except 1 .do file) you only need to
# download the following folders (and their contents) and files:
#       - data/1_raw/
#       - scripts/00_misc/
#       - scripts/0_functions/
#       - scripts/1_cleaning/
#       - scripts/2_formatting/
#       - scripts/3_analysis/
#       - replicate_all.R
#       - replication_marketing_taxation.Rproj
#
# PLEASE NOTE:
#   The replication_marketing_taxation.Rproj project must be open
#   in RStudio.
# ---------------------------------------------------------------------------- #

# File Management --------------------------------------------------------------
# check for data
if(!dir.exists("data/1_raw/")) {
  cat("WARNING: Without raw data, it will not be possible to run this replication script succesfully.")
}

check_scripts <- dir.exists(c("scripts/0_functions/", 
                              "scripts/1_cleaning/",
                              "scripts/2_formatting/",
                              "scripts/3_analysis/"
))

if(!all(check_scripts)) {
  cat("WARNING: One of the script directories is missing. This replication script will not run successfully.")
}

# create necessary folders if they do not exist
if(!dir.exists("data/2_clean/")) dir.create("data/2_clean/")
if(!dir.exists("data/3_formatted/")) dir.create("data/3_formatted/")
if(!dir.exists("output/")) dir.create("output/")
if(!dir.exists("output/appendix/")) dir.create("output/appendix/")
if(!dir.exists("output/main/")) dir.create("output/main/")

# Replicate Data Cleaning ------------------------------------------------------
clean_dir <- "scripts/1_cleaning/"

cleaning_scripts <- list.files(clean_dir)

# cannot source .do file
cleaning_scripts <- cleaning_scripts[!grepl(".do", cleaning_scripts)]

# this can take several minutes
# the time cost is the Data Exchange cleaning in 02cleaning_market_month.R
for(script in cleaning_scripts) {
  source(paste0(clean_dir, script))
}
# Replicate Data Formatting ----------------------------------------------------
format_dir <- "scripts/2_formatting/"

formatting_scripts <- list.files(format_dir)

# cannot source .do file
formatting_scripts <- formatting_scripts[!grepl(".do", formatting_scripts)]

# this will take a significant amount of time
# the time cost are the simulations in 02formatting_spillover.R
for(script in formatting_scripts) {
  source(paste0(format_dir, script))
}

# Replicate Analysis -----------------------------------------------------------

## Main ----
source("scripts/3_analysis/analysis_main.R")

## Appendix ----
source("scripts/3_analysis/analysis_appendix.R")